/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import com.aroundj2me.controller.ApplicationController;
import com.aroundj2me.controller.ScreenController;
import com.aroundj2me.model.CategoryName;
import com.aroundj2me.utility.ImageLoader;

public class CategoryListView extends List implements CommandListener, IView  {
	private ApplicationController controller = null;
	
	private Command selectCategory;
	
	public CategoryListView(){
		super("Categories", List.IMPLICIT);
		
		initialize();
		createCommands();
		
		this.setCommandListener(this);
	}

	private void initialize() {
		//TODO change image
		this.append(CategoryName.BANKS, ImageLoader.getCategoryImage(CategoryName.BANKS));
		this.append(CategoryName.BARS, ImageLoader.getCategoryImage(CategoryName.BARS));
		this.append(CategoryName.COFFEE, ImageLoader.getCategoryImage(CategoryName.COFFEE));
		this.append(CategoryName.GASSTATIONS, ImageLoader.getCategoryImage(CategoryName.GASSTATIONS));
		this.append(CategoryName.HOSPITALS, ImageLoader.getCategoryImage(CategoryName.HOSPITALS));
		this.append(CategoryName.HOTELS, ImageLoader.getCategoryImage(CategoryName.HOTELS));
		this.append(CategoryName.CINEMA, ImageLoader.getCategoryImage(CategoryName.CINEMA));
		this.append(CategoryName.PARKING, ImageLoader.getCategoryImage(CategoryName.PARKING));
		this.append(CategoryName.PHARMACIES, ImageLoader.getCategoryImage(CategoryName.PHARMACIES));
		this.append(CategoryName.PUBS, ImageLoader.getCategoryImage(CategoryName.PUBS));
		this.append(CategoryName.RESTAURANTS, ImageLoader.getCategoryImage(CategoryName.RESTAURANTS));
		this.append(CategoryName.SUPERMARKETS, ImageLoader.getCategoryImage(CategoryName.SUPERMARKETS));
		this.append(CategoryName.TAXIS, ImageLoader.getCategoryImage(CategoryName.TAXIS));
		this.append(CategoryName.THEATERS, ImageLoader.getCategoryImage(CategoryName.THEATERS));
	}	
	
	private void createCommands(){
		selectCategory = new Command("Select", "Select category", Command.SCREEN,0);
		
		this.addCommand(selectCategory);
	}


	public void commandAction(Command c, Displayable d) {
		if(c.getCommandType() == Command.BACK)
			ScreenController.getInstance().last();
		else if(d == this || c == selectCategory){
			controller.executeCategoryRequest();
		}
	}
	
	public String getSelectedCategory(){
		return this.getString(this.getSelectedIndex());
	}

	public void setController(ApplicationController controller) {
		this.controller = controller;
	}

}
